package com.sunday.haowushop.ui;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.sunday.common.base.BaseActivity;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.PixUtils;
import com.sunday.common.utils.SpannalbeStringUtils;
import com.sunday.common.utils.ToastUtils;
import com.sunday.common.widgets.ClearEditText;
import com.sunday.haowushop.R;
import com.sunday.haowushop.common.BaseApp;
import com.sunday.haowushop.http.ApiClient;
import com.sunday.haowushop.widgets.YunbiShareWindow;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.sunday.common.logger.Logger.init;

/**
 * Created by 刘涛 on 2017/7/10.
 */

public class YunbiShareActivity extends BaseActivity {


    @Bind(R.id.title)
    TextView title;
    @Bind(R.id.left_menu)
    ImageView leftMenu;
    @Bind(R.id.edit_num)
    EditText editNum;
    @Bind(R.id.max_num)
    TextView maxNum;
    @Bind(R.id.edit_remark)
    ClearEditText editRemark;
    @Bind(R.id.btn_share)
    TextView btnShare;
    @Bind(R.id.money)
    TextView shareMoney;

    private String money, shareTitle;
    private int id;
    private long memberId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_yunbi_share);
        ButterKnife.bind(this);
        title.setText("限时分享");
        leftMenu.setVisibility(View.VISIBLE);
        leftMenu.setImageResource(R.mipmap.back_white);
        id = getIntent().getIntExtra("id", 0);
        money = getIntent().getStringExtra("money");
        memberId = BaseApp.getInstance().getMember().getId();
        shareTitle = BaseApp.getInstance().getMember().getNickName();
        initView();


    }

    int maxShareNum;

    private void initView() {
        shareMoney.setText(SpannalbeStringUtils.setTextSizePx("¥", PixUtils.sp2px(mContext,13)));
        shareMoney.append(SpannalbeStringUtils.setTextSizePx(money,  PixUtils.sp2px(mContext,22)));
        shareMoney.append(SpannalbeStringUtils.setTextSizePx("云币",  PixUtils.sp2px(mContext,13)));
        int moneyNum = Integer.valueOf(money);
        maxShareNum = moneyNum % 5 == 0 ? moneyNum / 5 : moneyNum / 5 + 1;
        maxNum.setText(String.format("该云币最多可分享给%d位小伙伴哦", maxShareNum));
    }

    @OnClick(R.id.left_menu)
    void back() {
        onBackPressed();
    }


    String remark;

    @OnClick(R.id.btn_share)
    void shareYunbi() {
        String number = editNum.getText().toString().trim();
        remark = editRemark.getText().toString().trim();
        if (TextUtils.isEmpty(number)) {
            ToastUtils.showToast(mContext, "请输入领取人数");
            return;
        }
        if (maxShareNum < Integer.valueOf(number)) {
            ToastUtils.showToast(mContext, "领取人数不能超过最大人数哦");
            return;
        }
        showLoadingDialog(0);
        Call<ResultDO<Integer>> call = ApiClient.getApiAdapter().getShareId(id, Integer.valueOf(number), remark);
        call.enqueue(new Callback<ResultDO<Integer>>() {
            @Override
            public void onResponse(Call<ResultDO<Integer>> call, Response<ResultDO<Integer>> response) {
                dissMissDialog();
                if (isFinish || response.body() == null) {
                    return;
                }
                if (response.body().getCode() == 0) {
                    String shareUrl = String.format("%1$s%2$d-%3$d-%4$d-%5$d", ApiClient.SHARE_URL, 6, memberId, response.body().getResult(), 0);
                    YunbiShareWindow shareWindow = new YunbiShareWindow(mContext, shareUrl, String.format("%s给您发红包了", shareTitle), remark);
                    shareWindow.showPopupWindow(btnShare);
                }else {
                    ToastUtils.showToast(mContext,response.body().getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResultDO<Integer>> call, Throwable t) {
                dissMissDialog();
                ToastUtils.showToast(mContext, R.string.network_error);
            }
        });
    }


}
